package cn.rkylin.oms.refund.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.order.domain.EcOrder;
import cn.rkylin.oms.refund.domain.EcRefundItem;

@Service("taobaoRefundItemService")
public class TaobaoRefundItemService {

    @Resource(name = "redisTemplate")
    private RedisTemplate<Serializable, Serializable> redisTemplate;
    
    @Autowired
    private IDataBaseFactory dao;
    
    public int update(String updateStatement, EcRefundItem ecRefundItem) throws Exception {
        int result = dao.update(updateStatement, ecRefundItem);
        //redis 更新 按照交易号放置
        if (result!=0){
            updateRedis(ecRefundItem);
        }
        return result;
    }
    
    public int insert(String pageStatement, EcRefundItem ecRefundItem) throws Exception {
        int result = dao.insert(pageStatement, ecRefundItem);
        if (result!=0){
            updateRedis(ecRefundItem);
        }
        
        return result;
    }
    
    private void updateRedis(EcRefundItem ecRefundItem) throws Exception{
        
        List<String> list = null;
        //redis 更新
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        list = (ArrayList<String>)valueops.get("淘宝-RefundItem-EcRefundId:"+ecRefundItem.getEcRefundId());
        if (list!=null && list.size()>0){
            boolean flg = false; 
            for (int i = 0 ; i < list.size() ; i++){
                if (list.get(i).equals(ecRefundItem.getEcRefundItemId())){
                    flg = true;
                    break;
                }
            }
            if (!flg){
                list.add(ecRefundItem.getEcRefundItemId());
            }
        } else {
            list = new ArrayList<String>();
            list.add(ecRefundItem.getEcRefundItemId());
        }
        valueops.set("淘宝-Order-EcRefundId:"+ecRefundItem.getEcRefundId(), (Serializable)list); 
    }
    
    public List<EcRefundItem> getByecRefundId(String ecRefundId) throws Exception {
        
        if (StringUtils.isBlank(ecRefundId)){
            return new ArrayList<EcRefundItem>();
        }
        List<EcRefundItem> listRefundItem = new ArrayList<EcRefundItem>(); 
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        List<String> listRefundItemId = (ArrayList<String>)valueops.get("淘宝-RefundItem-EcRefundId:"+ecRefundId);
        if (listRefundItemId!=null && listRefundItemId.size()>0){
            for (int j = 0 ; j < listRefundItemId.size() ; j++){
                listRefundItem.add((EcRefundItem)dao.find("selectByPrimaryKeyEcRefundItem", listRefundItemId.get(j)));
            }
        }
        
      //如果redis中没有取到就从数据库中取得
        if (listRefundItem==null || listRefundItem.size()<=0){
            List<EcRefundItem> ecRefundItems = new ArrayList<EcRefundItem>();
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("ecRefundId", ecRefundId);
            ecRefundItems = dao.findForList("selectbyEcRefundId", param);
            listRefundItem.addAll(ecRefundItems);
            List<String> list = new ArrayList<String>();
            for (int j = 0 ; j < ecRefundItems.size() ; j++){
                list.add(ecRefundItems.get(j).getEcRefundItemId());
            }
            valueops.set("淘宝-RefundItem-EcRefundId:"+ecRefundId, (Serializable)list); 
        }
        
        return listRefundItem;
    }


    /**
     * 根据refundId获取EcRefund
     * @param refundId
     * @return
     * @throws Exception
     */
    public EcRefundItem selectEcRefundItemByRefundId(String refundId) throws Exception{
        EcRefundItem ecRefundItem = (EcRefundItem)dao.find("selectEcRefundItemByRefundId",refundId);
        return ecRefundItem;
    }
}
